local hx__cengxiangshi = fk.CreateSkill {
  name = "hx__cengxiangshi",
}


hx__cengxiangshi:addEffect(fk.CardUsing, {
  name = "hx__cengxiangshi",
  anim_type = "offensive",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__cengxiangshi.name) and data.card and not table.contains(data.card.skillNames, hx__cengxiangshi.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.card.type == Card.TypeBasic then
      local card1 = table.filter(player:getCardIds("he"), function(id) return Fk:getCardById(id).type == Card.TypeEquip or Fk:getCardById(id).type == Card.TypeTrick end)
      if #card1 == 0 then return end
      local discard1 = room:askToDiscard(player,{
        min_num = 1,
        max_num = 1,
        include_equip = true,
        cancelable = false,
        skill_name = hx__cengxiangshi.name,
        pattern = ".|.|.|.|.|trick,equip",
        prompt = "#hx__cengxiangshi-ask",
      })
      if #discard1 == 0 then return end
      if Fk:getCardById(discard1[1]).type == Card.TypeEquip then
        room:askToUseVirtualCard(player,{
          name = Fk:getAllCardNames("t"),
          skill_name = hx__cengxiangshi.name,
          prompt = "#hx__cengxiangshi-target",
          cancelable = false,
        })
      elseif Fk:getCardById(discard1[1]).type == Card.TypeTrick then
        local cid1 = room:askToCards(player,{
          min_num = 0,
          max_num = 1,
          flag = "he",
          skill_name = hx__cengxiangshi.name,
          prompt = "#hx__cengxiangshi-recast",
        })
        if #cid1 > 0 then
          room:recastCard(cid1,player,hx__cengxiangshi.name)
        end
      end
    elseif data.card.type == Card.TypeTrick then
      local card2 = table.filter(player:getCardIds("he"), function(id) return Fk:getCardById(id).type == Card.TypeEquip or Fk:getCardById(id).type == Card.TypeBasic end)
      if #card2 == 0 then return end
      local discard2 = room:askToDiscard(player,{
        min_num = 1,
        max_num = 1,
        include_equip = true,
        cancelable = false,
        skill_name = hx__cengxiangshi.name,
        pattern = ".|.|.|.|.|basic,equip",
        prompt = "#hx__cengxiangshi-ask",
      })
      if #discard2 == 0 then return end
      if Fk:getCardById(discard2[1]).type == Card.TypeEquip then
        room:askToUseVirtualCard(player,{
          name = Fk:getAllCardNames("b"),
          skill_name = hx__cengxiangshi.name,
          prompt = "#hx__cengxiangshi-target",
          cancelable = false,
        })
      elseif Fk:getCardById(discard2[1]).type == Card.TypeBasic then
        local cid2 = room:askToCards(player,{
          min_num = 0,
          max_num = 1,
          flag = "he",
          skill_name = hx__cengxiangshi.name,
          prompt = "#hx__cengxiangshi-recast",
        })
        if #cid2 > 0 then
          room:recastCard(cid2,player,hx__cengxiangshi.name)
        end
      end
    elseif data.card.type == Card.TypeEquip then
      local card3 = table.filter(player:getCardIds("he"), function(id) return Fk:getCardById(id).type == Card.TypeTrick or Fk:getCardById(id).type == Card.TypeBasic end)
      if #card3 == 0 then return end
      local discard3 = room:askToDiscard(player,{
        min_num = 1,
        max_num = 1,
        include_equip = true,
        cancelable = false,
        skill_name = hx__cengxiangshi.name,
        pattern = ".|.|.|.|.|basic,trick",
        prompt = "#hx__cengxiangshi-ask",
      })
      if #discard3 == 0 then return end
      if Fk:getCardById(discard3[1]).type == Card.TypeTrick then
        room:askToUseVirtualCard(player,{
          name = Fk:getAllCardNames("b"),
          skill_name = hx__cengxiangshi.name,
          prompt = "#hx__cengxiangshi-target",
          cancelable = false,
        })
      elseif Fk:getCardById(discard3[1]).type == Card.TypeBasic then
        room:askToUseVirtualCard(player,{
          name = Fk:getAllCardNames("t"),
          skill_name = hx__cengxiangshi.name,
          prompt = "#hx__cengxiangshi-target",
          cancelable = false,
        })
      end
    end
  end,
})

return hx__cengxiangshi